home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / cli / UnixUtils.lha / UnixUtils / Source / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-26  |  3.0 KB  |  120 lines

  1. /*------------------------------------------------------*
  2.  | Strings.c - Unix-like utility.                       |
  3.  | Syntax: strings [-N] [-A] [-L] [file [file [ ... ] ] |
  4.  | Prints on stdout the "strings" found in the input    |
  5.  | file(s), or in stdin; a "string" is a sequence of at |
  6.  | least N (default: N_DEFVAL) printing characters,     |
  7.  | ending with a newline or a NULL; or a sequence of at |
  8.  | least N printable characters if the switch -A (for   |
  9.  | ALL) has been given. If -L is present, printable     |
  10.  | characters are limited to the range 0x0-0x7F, NULL   |
  11.  | to DEL, and do not include 8-bit characters.         |
  12.  | v1.00 MLO 920325                                     |
  13.  *------------------------------------------------------*/
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include "mlo.h"
  19.  
  20. #define N_DEFVAL      4
  21. #define LINE_LENGTH   256
  22. #define DEL           0177
  23.  
  24. void DoTheStuff(FILE *fp, int n, Boolean all, Boolean low);
  25. void Syntax(void);
  26.  
  27. void main(
  28.   int argc,
  29.   char **argv
  30. ){
  31.   int nChars = N_DEFVAL;
  32.   Boolean all = False;
  33.   Boolean low = False;
  34.  
  35.   while (--argc) {
  36.     if ( ((*++argv)[0] == '-') ) {
  37.       if (isdigit((*argv)[1])) {
  38.         if ( (nChars = atoi(*argv+1)) < 1) Syntax();
  39.       } else {
  40.         switch ((*argv)[1]) {
  41.           case 'a':
  42.           case 'A':
  43.             all = True;
  44.             break;
  45.           case 'l':
  46.           case 'L':
  47.             low = True;
  48.             break;
  49.           default:
  50.             Syntax();
  51.         }
  52.       }
  53.     } else if ((*argv)[0] == '?') {
  54.       Syntax();
  55.     } else {
  56.       break;
  57.     }
  58.   }
  59.  
  60.   if (argc) {
  61.     while (argc--) {
  62.       FILE *fp;
  63.  
  64.       if ( (fp = fopen(*argv, "r")) == NULL) {
  65.         printf("strings: couldn't open file \"%s\".\n", *argv);
  66.       } else {
  67.         DoTheStuff(fp, nChars, all, low);
  68.         fclose(fp);
  69.       }
  70.       ++argv;
  71.     }
  72.   } else {
  73.     DoTheStuff(stdin, nChars, all, low);
  74.   }
  75.  
  76.   exit(SYS_NORMAL_CODE);
  77. }
  78.  
  79. void DoTheStuff(
  80.   FILE *fp,
  81.   int n,
  82.   Boolean all,
  83.   Boolean low
  84. ){
  85.   char line[LINE_LENGTH];
  86.   char *pc = line;
  87.   int c;
  88.  
  89.   while ( (c =fgetc(fp)) != EOF) {
  90.     if (isprint(c)   &&   (!low  ||  c < DEL)) {
  91.       *pc++ = c;
  92.     } else {
  93.       if ((pc - line) >= n) {
  94.         if (all   ||   (c == NEWLINE  ||  c == NIHIL)) {
  95.           *pc = NIHIL;
  96.           puts(line);
  97.         }
  98.       }
  99.       pc = line;
  100.     }
  101.   }
  102. }
  103.  
  104. void Syntax(void)
  105. {
  106.   puts("\nUsage:   strings [-N] [-A] [-L] [file [file [ ... ]]]");
  107.   puts("Purpose: prints on stdout the \"strings\" found in the given");
  108.   puts("         file(s), or in stdin. A \"string\" is defined as a");
  109.   printf("         sequence of at least N (default: %d) printable\n",
  110.          N_DEFVAL);
  111.   puts("         characters ended from newline or a NULL; or as a");
  112.   puts("         sequence of N printable characters, if the switch");
  113.   puts("         -A (for ALL) has been given. The switch -L limits");
  114.   puts("         printable characters to the range 0x0-0x7F, NULL");
  115.   puts("         to DEL, and do not consider printables 8 bit");
  116.   puts("         characters.\n");
  117.  
  118.   exit(SYS_NORMAL_CODE);
  119. }
  120.